package Form;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;

import Control.CommonControls;
import Control.Customer_Controller;
import Entity.Customer_sendSms;
import Entity.User;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class Customer_PasswordRecovery extends JPanel {
	private JLabel lb_des1;
	private JPanel pn_NRIC;
	private JLabel lb_des2;
	private JTextField tb_NRIC;
	private JPanel pn_security;
	private JPanel pn_code;
	private JLabel lb_des3;
	private JLabel lb_secqn;
	private JTextField tb_ans;
	private JLabel lb_des4;
	private JLabel lb_des5;
	private JTextField tb_code;
	private JButton bn_RegCheck;
	private JButton btnMatchAns;
	private JButton bn_submitCode;
	private JPanel pn_choice;
	private JPanel pn_resetPwd;
	private JLabel lblPassword;
	private JLabel lblConfirmPassword;
	private JPasswordField pf_Pwd;
	private JPasswordField pf_CfmPwd;
	private JLabel lblNewLabel;
	private JButton bn_resetPwd;
	private JFrame parser;
	private User u = new User();
	private Customer_Controller control = new Customer_Controller();
	private String code = null;
	private JButton btn_SecQnAns;
	private JButton btn_Code;
	private JLabel lb_CodeError;
	private JLabel lb_ErrorSA;
	private JLabel lb_ErrorPwd;
	private JLabel lb_errorNric;
	
	public JFrame getParser() {
		return parser;
	}
	
	public void setParser(JFrame parser){
		this.parser = parser;
	}	
	
	public Customer_Controller getControl(){
		return control;
	}

	public void setControl (Customer_Controller control) {
		this.control = control; 
	}
	
	public Customer_PasswordRecovery(JFrame parser) {

		setLayout(null);
		add(getPn_NRIC());
		add(getPn_security());
		add(getPn_choice());
		add(getPn_resetPwd());
		add(getPn_code());

	}
	private JLabel getLb_des1() {
		if (lb_des1 == null) {
			lb_des1 = new JLabel("You can recover your password via ");
			lb_des1.setBounds(6, 1, 199, 14);
		}
		return lb_des1;
	}
	private JPanel getPn_NRIC() {
		if (pn_NRIC == null) {
			pn_NRIC = new JPanel();
			pn_NRIC.setBounds(46, 28, 362, 94);
			pn_NRIC.setLayout(null);
			pn_NRIC.add(getLb_des2());
			pn_NRIC.add(getTb_NRIC());
			pn_NRIC.add(getBn_RegCheck());
			pn_NRIC.add(getLb_errorNric());
		}
		return pn_NRIC;
	}
	private JLabel getLb_des2() {
		if (lb_des2 == null) {
			lb_des2 = new JLabel("Please enter your NRIC or Company Registration number");
			lb_des2.setBounds(10, 11, 341, 14);
		}
		return lb_des2;
	}
	private JTextField getTb_NRIC() {
		if (tb_NRIC == null) {
			tb_NRIC = new JTextField();
			tb_NRIC.setBounds(10, 36, 182, 20);
			tb_NRIC.setColumns(10);
		}
		return tb_NRIC;
	}
	private JPanel getPn_security() {
		if (pn_security == null) {
			pn_security = new JPanel();
			pn_security.setBounds(46, 201, 362, 121);
			pn_security.setLayout(null);
			pn_security.add(getLb_des3());
			pn_security.add(getLb_secqn());
			pn_security.add(getTb_ans());
			pn_security.add(getBtnMatchAns());
			pn_security.add(getLb_ErrorSA());
			pn_security.setVisible(false);
		}
		return pn_security;
	}
	private JPanel getPn_code() {
		if (pn_code == null) {
			pn_code = new JPanel();
			pn_code.setBounds(46, 201, 377, 110);
			pn_code.setLayout(null);
			pn_code.add(getLb_des4());
			pn_code.add(getLb_des5());
			pn_code.add(getTb_code());
			pn_code.add(getBn_submitCode());
			pn_code.add(getLb_CodeError());
			pn_code.setVisible(false);
		}
		return pn_code;
	}
	private JLabel getLb_des3() {
		if (lb_des3 == null) {
			lb_des3 = new JLabel("Please enter your answer to your security question ");
			lb_des3.setBounds(10, 11, 261, 14);
		}
		return lb_des3;
	}
	private JLabel getLb_secqn() {
		if (lb_secqn == null) {
			lb_secqn = new JLabel("Security Qn");
			lb_secqn.setBounds(10, 36, 295, 14);
		}
		return lb_secqn;
	}
	private JTextField getTb_ans() {
		if (tb_ans == null) {
			tb_ans = new JTextField();
			tb_ans.setBounds(10, 61, 168, 20);
			tb_ans.setColumns(10);
		}
		return tb_ans;
	}
	private JLabel getLb_des4() {
		if (lb_des4 == null) {
			lb_des4 = new JLabel("A code has been sent to your phone. \r\n");
			lb_des4.setBounds(10, 11, 212, 24);
		}
		return lb_des4;
	}
	private JLabel getLb_des5() {
		if (lb_des5 == null) {
			lb_des5 = new JLabel(" Please enter the codes below");
			lb_des5.setBounds(10, 35, 177, 14);
		}
		return lb_des5;
	}
	private JTextField getTb_code() {
		if (tb_code == null) {
			tb_code = new JTextField();
			tb_code.setBounds(10, 58, 101, 20);
			tb_code.setColumns(10);
		}
		return tb_code;
	}

	private JPanel getPn_choice() {
		if (pn_choice == null) {
			pn_choice = new JPanel();
			pn_choice.setBounds(46, 133, 362, 68);
			pn_choice.setLayout(null);
			pn_choice.add(getLb_des1());
			pn_choice.add(getBtn_SecQnAns());
			pn_choice.add(getBtn_Code());
			pn_choice.setVisible(false);
		}
		return pn_choice;
	}
	private JPanel getPn_resetPwd() {
		if (pn_resetPwd == null) {
			pn_resetPwd = new JPanel();
			pn_resetPwd.setBounds(46, 343, 362, 165);
			pn_resetPwd.setLayout(null);
			pn_resetPwd.add(getLblPassword());
			pn_resetPwd.add(getLblConfirmPassword());
			pn_resetPwd.add(getPf_Pwd());
			pn_resetPwd.add(getPf_CfmPwd());
			pn_resetPwd.add(getLblNewLabel());
			pn_resetPwd.add(getBn_resetPwd());
			pn_resetPwd.add(getLb_ErrorPwd());
			pn_resetPwd.setVisible(false);
		}
		return pn_resetPwd;
	}
	private JLabel getLblPassword() {
		if (lblPassword == null) {
			lblPassword = new JLabel("New Password ");
			lblPassword.setBounds(10, 43, 84, 14);
		}
		return lblPassword;
	}
	private JLabel getLblConfirmPassword() {
		if (lblConfirmPassword == null) {
			lblConfirmPassword = new JLabel("Confirm Password");
			lblConfirmPassword.setBounds(10, 68, 103, 14);
		}
		return lblConfirmPassword;
	}
	private JPasswordField getPf_Pwd() {
		if (pf_Pwd == null) {
			pf_Pwd = new JPasswordField();
			pf_Pwd.setBounds(143, 40, 130, 20);
		}
		return pf_Pwd;
	}
	private JPasswordField getPf_CfmPwd() {
		if (pf_CfmPwd == null) {
			pf_CfmPwd = new JPasswordField();
			pf_CfmPwd.setBounds(143, 65, 130, 20);
		}
		return pf_CfmPwd;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("You can now reset your password ");
			lblNewLabel.setBounds(10, 11, 263, 14);
		}
		return lblNewLabel;
	}
	
	

	
	private void generateCode(){
		code = CommonControls.generateDefaultType5DigitCode('.');
		code = code.substring(1);
		Customer_sendSms sms = new Customer_sendSms();
		sms.send(u.getContact_no()+"", "Customer Password Reset Code : "+ code +". Please use enter this code to the system to reset your password");
		pn_code.setVisible(true);
		//TEST>>>sms.send(91308300+"", "Hi, this is your code : "+ code);
		//sendSms() <-two parkameters, phone num and message to be sent
	}
	private JButton getBtn_SecQnAns() {
		if (btn_SecQnAns == null) {
			btn_SecQnAns = new JButton("Security Question");
			btn_SecQnAns.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					btn_SecQnAns.setEnabled(false);
					btn_Code.setEnabled(false);
					pn_security.setVisible(true);
										
				}
			});
			btn_SecQnAns.setBounds(16, 26, 117, 23);
		}
		return btn_SecQnAns;
	}
	private JButton getBtn_Code() {
		if (btn_Code == null) {
			btn_Code = new JButton("Sms Code");
			btn_Code.setEnabled(true);
			btn_Code.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					btn_SecQnAns.setEnabled(false);
					btn_Code.setEnabled(true);
					pn_code.setVisible(true);
					
					generateCode();
				}
			});
			btn_Code.setBounds(151, 26, 117, 23);
		}
		return btn_Code;
	}
	private JLabel getLb_CodeError() {
		if (lb_CodeError == null) {
			lb_CodeError = new JLabel("You have enter the wrong code. Please try again ");
			lb_CodeError.setForeground(Color.RED);
			lb_CodeError.setFont(new Font("Tahoma", Font.BOLD, 11));
			lb_CodeError.setBounds(10, 86, 286, 14);
			lb_CodeError.setVisible(false);
		}
		return lb_CodeError;
	}
	private JLabel getLb_ErrorSA() {
		if (lb_ErrorSA == null) {
			lb_ErrorSA = new JLabel("Invalid answer. Please try again");
			lb_ErrorSA.setFont(new Font("Tahoma", Font.BOLD, 11));
			lb_ErrorSA.setForeground(Color.RED);
			lb_ErrorSA.setBounds(10, 96, 241, 14);
			lb_ErrorSA.setVisible(false);
		}
		return lb_ErrorSA;
	}
	private JLabel getLb_ErrorPwd() {
		if (lb_ErrorPwd == null) {
			lb_ErrorPwd = new JLabel("You password does not match. Please try again");
			lb_ErrorPwd.setForeground(Color.RED);
			lb_ErrorPwd.setFont(new Font("Tahoma", Font.BOLD, 11));
			lb_ErrorPwd.setBounds(10, 140, 322, 14);
			lb_ErrorPwd.setVisible(false);
			
		}
		return lb_ErrorPwd;
	}
	private JLabel getLb_errorNric() {
		if (lb_errorNric == null) {
			lb_errorNric = new JLabel("Please enter your NRIC or Company Registration Number");
			lb_errorNric.setFont(new Font("Tahoma", Font.BOLD, 11));
			lb_errorNric.setForeground(Color.RED);
			lb_errorNric.setBounds(10, 69, 341, 14);
			lb_errorNric.setVisible(false);
		}
		return lb_errorNric;
	}
	
	public boolean validateSecurityQns(){
		boolean valid = false;
		if(u.getSecurity_ans().equals(tb_ans.getText())){
			valid = true;
			
		}else {
			tb_ans.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
			lb_ErrorSA.setVisible(true);
			lb_ErrorSA.setText("Invalid answer. Please try again");
			
		}
		return valid;
	}
	
	public boolean validateCode(){
		boolean valid = false;

		if(code.equals(tb_code.getText())){
			valid = true;
			
		}else {
			tb_code.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
			lb_CodeError.setVisible(true);
			lb_CodeError.setText("You have enter the wrong code. Please try again");
		}
		return valid;
	}
	private JButton getBn_RegCheck() {
		if (bn_RegCheck == null) {
			bn_RegCheck = new JButton("Submit");
			bn_RegCheck.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(tb_NRIC.getText().isEmpty()){
						tb_NRIC.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
						lb_errorNric.setVisible(true);
						lb_errorNric.setText("Please enter your NRIC or Company Registration Number");
					}
					else {
							u = control.retrieveSecQnsAns(tb_NRIC.getText());
						
							if(u!=null){
							lb_secqn.setText(u.getSecurity_qn());
							pn_choice.setVisible(true);
							tb_NRIC.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
							}
							else{
								tb_NRIC.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
								lb_errorNric.setVisible(true);
								lb_errorNric.setText("Invalid Input. Please enter a registered NRIC or Company registration Number");
							}
					}
				}
			});
			bn_RegCheck.setBounds(202, 33, 89, 23);
		}
		return bn_RegCheck;
	}
	private JButton getBtnMatchAns() {
		if (btnMatchAns == null) {
			btnMatchAns = new JButton("Match answer! ");
			btnMatchAns.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					boolean valid = validateSecurityQns();
					if(tb_ans.getText().isEmpty()){
						tb_ans.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
						lb_ErrorSA.setVisible(true);
						lb_ErrorSA.setText("Please enter the Security Answer");
					}
					else {
						if(valid == true){
							pn_resetPwd.setVisible(true);
							tb_ans.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
							
						}
					}
				}
			});
			btnMatchAns.setBounds(215, 60, 107, 23);
		}
		return btnMatchAns;
	}
	
	
	private JButton getBn_submitCode() {
		if (bn_submitCode == null) {
			bn_submitCode = new JButton("Submit code");
			bn_submitCode.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					boolean valid = validateCode();
					if(tb_code.getText().isEmpty()){
						tb_code.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
						lb_CodeError.setVisible(true);
						lb_CodeError.setText("Please enter code");
					}
					else {
						if(valid == true){
						pn_resetPwd.setVisible(true);
						tb_code.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
						}
					}
				}
			});
			bn_submitCode.setBounds(121, 57, 101, 23);
		}
		return bn_submitCode;
	}
	
	private JButton getBn_resetPwd() {
		if (bn_resetPwd == null) {
			bn_resetPwd = new JButton("Submit");
			bn_resetPwd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				
					if(new String(pf_Pwd.getPassword()).equals(new String(pf_CfmPwd.getPassword()))==false){ 
						//careful if here got encap error, result in password not tally
						//prompt new password not tally error
						pf_Pwd.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
						pf_CfmPwd.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
						lb_ErrorPwd.setVisible(true);
						
					}else{
						u.setPassword(new String(pf_Pwd.getPassword()));
						control.resetPassword(u);
						pf_Pwd.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
						pf_CfmPwd.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
						JOptionPane.showMessageDialog(null ,"Successfully Update Password.");
						//parser.dispose();
						
					}
				}
			});
			bn_resetPwd.setBounds(143, 102, 142, 23);
		}
		return bn_resetPwd;
	}
}
